//
//  CustomerPickerView.m
//  PickerDemo
//
//  Created by Cluy on 16/12/19.
//  Copyright © 2016年 Cluy. All rights reserved.
//

#import "CustomerPickerView.h"

@implementation CustomerPickerView
- (instancetype)initWithColumn:(NSArray*)itemList titleKey:(NSString *)key{
    self = [self initWithFrame:CGRectZero];
    if (self) {
        //        --------------设置ToolBar 添加【取消】【确定】两个Btn-----------------------
        _actionToolbar = [[UIToolbar alloc]init];
        [_actionToolbar setBackgroundImage:[UIImage imageNamed:@"rl_carModel_picker_bg.png"] forToolbarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
        _actionToolbar.barStyle = UIBarStyleDefault;
        [_actionToolbar sizeToFit];
        
        CGRect toolBarFrame = _actionToolbar.frame;
        toolBarFrame.size.height = 44;
        _actionToolbar.frame = toolBarFrame;
        
        //        --------------set btn-----------------------
        NSMutableArray *btnItems = [[NSMutableArray alloc]init];
        UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"rl_carModel_picker_cancel.png"]  style: UIBarButtonItemStylePlain target:self action:@selector(CancelTouched:)];
        cancelBtn.tintColor = [UIColor blackColor];
        [btnItems addObject:cancelBtn];
        
        //添加btn 保持间距
        UIBarButtonItem *leftNilButton =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        [btnItems addObject:leftNilButton];
        
        UIBarButtonItem *rightNilButton =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        [btnItems addObject:rightNilButton];
        
        
        UIBarButtonItem *downBtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"rl_carModel_picker_done.png"]  style: UIBarButtonItemStylePlain target:self action:@selector(DoneTouched:)];
        downBtn.tintColor = [UIColor blackColor];
        [btnItems addObject:downBtn];
        [_actionToolbar setItems:btnItems];
        [self addSubview:_actionToolbar];
        
        //        --------------set PickerView-----------------------
        _customerPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(_actionToolbar.frame) , CGRectGetWidth(_actionToolbar.frame), 200)];
        _customerPicker.backgroundColor = [UIColor whiteColor];
        [_customerPicker setShowsSelectionIndicator:YES];
        [_customerPicker setDelegate:self];
        [_customerPicker setDataSource:self];
        [self addSubview:_customerPicker];
        
        self.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.8];
        [self setFrame:CGRectMake(0, 0, CGRectGetWidth(_customerPicker.frame), CGRectGetMaxY(_customerPicker.frame))];
        
        self.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleWidth;
        _actionToolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        _customerPicker.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        _itemList = [[NSMutableArray alloc]init];
        _itemList = itemList;
        _titleKey = key;
    }
    return self;
}
#pragma mark Cancel & Done
-(void)CancelTouched:(UIBarButtonItem*)barButton
{
    if (_didCancelAction) {
        _didCancelAction();
    }
}
-(void)DoneTouched:(UIBarButtonItem*)barButton
{
    if (_didSelectAction) {
        if (!_selectedTitles&&_itemList.count>0) {
            _selectedTitles = [[NSMutableArray alloc]init];
            [_selectedTitles addObject:[_itemList objectAtIndex:0]];
        }
        _didSelectAction(_selectedTitles);
    }
}
#pragma mark Picker Delegate&dataSource
- (NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}
- (NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [_itemList count];
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (_titleKey == nil || _titleKey.length == 0) {
        return [_itemList objectAtIndex:row];
    }else{
        NSDictionary *tempDic = [_itemList objectAtIndex:row];
        return [tempDic objectForKey:_titleKey];
    }
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        pickerLabel.font = [UIFont boldSystemFontOfSize:12];
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
    }
    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}

- (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    _selectedTitles = [[NSMutableArray alloc]init];
    [_selectedTitles addObject:[_itemList objectAtIndex:row]];
}
@end
